<div class='weui-cells' id="login-wechat-qrcode">
	<div class='weui-cell'>
		<div class='weui-cell__bd' style="text-align: center;">
			<div id='login-wechat-qrcode-container' style='height:190px;width:190px;background:#eee;margin:10px auto 0 auto;'>
				<div id="login-wechat-success" class='weui-icon weui-icon-success weui-icon_msg' style='padding-top:45px;display: none;'></div>
				<div id="login-wechat-error" class='weui-icon weui-icon-warn weui-icon_msg' style='padding-top:45px;display: none;'></div>
				<img id="login-wechat-qrcode-img" style="display:none;width:190px;height:190px;" />
			</div>
			<p mute id="login-wechat-checking" style='line-height:5px;vertical-align: top;' mute><br/></p>
			<small id="login-wechat-qrcode-cool" mute><d style='display: none'><i class='weui-loading'></i> 正在加载二维码</d><a id="info">轻击二维码刷新</a><c style='display:none'>，请稍后重试</c></small>
		</div>
	</div>
	<label class="weui-cell weui-check__label">
		<div class="weui-cell__hd">
			<input checked="checked" type="checkbox" class="weui-check" name="auto">
			<i class="weui-icon weui-icon-checked"></i>
		</div>
		<div class="weui-cell__bd" id="aaa">
			<small>自动登录</small>
		</div>
	</label>
</div>
<script>
	login_wechat = {
		container:$("#login-wechat-qrcode-container"),
		qrcodeimg:$("#login-wechat-qrcode-img"),
		error:$("#login-wechat-error"),
		success:$("#login-wechat-success"),
		loading:false,
		code:null,
		init:function(){
			this.container.on('click',function(){
				login_wechat.newQrcode();
			})
		},
		newQrcode:function(){
			this.stop();
			$("#login-wechat-qrcode-cool d").show();
			$("#login-wechat-qrcode-cool a").hide();
			$.ajax({
				url:"/login/qrcode_create",
                xhrFields:{
                    withCredentials:true
                },
				success:function(res){
					$("#login-wechat-qrcode-cool a").show();
					$("#login-wechat-qrcode-cool d").hide();
					if(res.data && res.data.remain){
						TIMER.start("#login-wechat-qrcode-cool",res.data.remain);
					}
					if(!res.state){
						$("#login-wechat-qrcode-cool c").show();
                        if(res.errormsg == 'already has wechat'){
                            window.location.href = 'http://api.hongyanche.com/home#my';
							return
                        }
						ERROR.top('二维码生成',res.errormsg);
						return;
					}
					login_wechat.qrcodeimg.prop("src",res.data.url);
					login_wechat.qrcodeimg.fadeIn();
					$("#login-wechat-checking small").remove();
					login_wechat.start(res.data.code);
				},
				error:function(e){
					error.top("二维码",e.statusText);
					$("#login-wechat-qrcode-cool a").show();
					$("#login-wechat-qrcode-cool d").hide();
					login_wechat.stop();
				}
			})
		},
		start:function(code){
			this.code = code;
			if(this.loading) return;
			this.loading = true;
			this.check();
		},
		check:function(){
			var self = this;
			if(!self.loading) return;
			$.ajax({
				url:"/login/qrcode_check",
				data:{code:self.code},
                xhrFields:{
                    withCredentials:true
                },
				success:function(res){
					console.log(res);
                    if(!self.loading) return;
                    if(!res.state){
                        if(res.errormsg == 'not responded'){
                                
                            var dots = "";
							var dotslen = $("#login-wechat-checking").html().length;
							if( dotslen > 5 ) dotslen = 0;
							for(var i =0 ; i < dotslen + 1 ;i++){
								dots = dots + ".";
							}
							$("#login-wechat-checking").html(dots);
							setTimeout(function(){
								login_wechat.check();
							},500);
							return;
						}

						self.stop();
						var errormsg = self.msg[res.errormsg] || res.errormsg;
						error.top("扫描",errormsg)
						$("#login-wechat-checking").html("<small style='color:maroon'>"+errormsg+"</small>");
						self.error.fadeIn();
						return;
					}
					self.stop();
					TOAST.success("扫描成功");
					self.success.fadeIn();
					setTimeout(function(){
						$(document).trigger('login-success');
						$(document).trigger('login-success-wechat');
					},1500)
				},
				error:function(e){
					self.stop();
					self.error.fadeIn();
					$("#login-wechat-checking").html("<small style='color:maroon'>"+e.statusText+"</small>");
					error.top("服务器",e.statusText);
				}
			})
		},
		stop:function(){
			this.code = null;
			this.loading = false;
			$("#login-wechat-checking").html('.');
			this.qrcodeimg.hide();
			this.error.hide();
			this.success.hide();
			$("#login-wechat-qrcode-loading").removeAttr("style")
			$("#login-wechat-qrcode-cool c").hide();
		},
		msg:{
			"wechat already binding":"微信已被使用"
		}
	}
	$(function(){
		login_wechat.init();
	});
</script>